#include "corpus.h"
#include "jobqueue.h"

bool SimpleCorpus::Load(string fn){
  Memblock b = Mmap2(fn);
  if (b.Fail()) return false;
  size_ = *((uint64 *) b.begin());
  data_ = (uint32 *) (b.begin() + sizeof(uint64));
  return true;
}

Memblock SimpleCorpus::GetMemblock() {
  return Memblock((const char *) (data_-2), (size_ + 2) * sizeof(uint32));
}

void SimpleCorpus::Store(string fn) {
  *((uint64 *)(data_-2)) = size_;
  File::Write(fn, GetMemblock());
}

void SimpleCorpus::DistributedTouch() {
  Memblock m = GetMemblock();
  ::DistributedTouch(m.begin(), m.size());
}

void SimpleCorpus::StartBuild(uint64 max_size) {
  max_size_ = max_size_;
  data_ = (new uint32[max_size + 2]) + 2;
  size_ = 0;
}

void SimpleCorpus::Push(uint32 n) {
  data_[size_++] = n;
  CHECK(size_ <= max_size_);

  if (size_ % 1000000 == 0)
    cout << "Building corpus size=" << size_ << endl;
}
void SimpleCorpus::EndBuild() {}
